//
//  proinfo.swift
//  WxToutiao
//
//  Created by YQ05 on 2018/4/27.
//  Copyright © 2018年 yons. All rights reserved.
//

import Foundation
import Moya
import ObjectMapper

/* Codable 版本代码 */
//struct ProInfoResponse: Codable {
//    var products : [Product]!
//}
//
//
//struct Product: Codable {
//    var ID = 0
//    var name = ""
//    var desc = ""
//
//    var price = ""
//    var regprice = ""
//    var image : Array<Any>
//}

//struct ProInfoResponse: Codable {
//    let products: [Product]?
//}
//
//struct Product: Codable {
//    let id: Int?
//    let name, desc, price, regprice: String?
//    let image: [Image]?
//
//    enum CodingKeys: String, CodingKey {
//        case id = "ID"
//        case name, desc, price, regprice, image
//    }
//}
//
//enum Image: Codable {
////    case bool(Bool)
////    case integer(Int)
//    case string(String)
//
//    init(from decoder: Decoder) throws {
//        let container = try decoder.singleValueContainer()
////        if let x = try? container.decode(Bool.self) {
////            self = .bool(x)
////            return
////        }
////        if let x = try? container.decode(Int.self) {
////            self = .integer(x)
////            return
////        }
//        if let x = try? container.decode(String.self) {
//            self = .string(x)
//            return
//        }
//        throw DecodingError.typeMismatch(Image.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Image"))
//    }
//
//    func encode(to encoder: Encoder) throws {
//        var container = encoder.singleValueContainer()
//        switch self {
////        case .bool(let x):
////            try container.encode(x)
////        case .integer(let x):
////            try container.encode(x)
//        case .string(let x):
//            try container.encode(x)
//        }
//    }
//}

/* Codable 版本代码 */

struct proInfoResponse: Mappable {

    var products : [Product]!

    init?(map:Map) {

    }
    mutating func mapping(map: Map) {
        products    <- map["products"]
    }

}

struct Product: Mappable {
    var ID : Int?
    var name:String?
    var desc:String?

    var img: [Any]?
    var image :String?

    var price : String?
    var regprice :String?


    init?(map: Map) {

    }

    mutating func mapping(map: Map) {
        ID     <- map["ID"]
        name    <- map["name"]
        desc    <- map["desc"]

        img     <- map["image"]
        image = img![0] as? String

        price     <- map["price"]
        regprice     <- map["regprice"]
    }
}



extension Product {
    /// 获取分类下的文章列表
    static func request(id: Int,completion: @escaping ([Product]?) -> Void ) {
        
        let provider = MoyaProvider<NetworkService2>()
        
        provider.request(.getProCateList(id: id)) { (result) in
            switch result {
            case let .success(moyaResponse):
                
                do {
                    /* ObjectMapper  版本*/
                    let json = try moyaResponse.mapJSON() as! [String:Any]
                    let jsonResponse = proInfoResponse(JSON: json)
                    print("proinfo.swift商品列表json \(String(describing: jsonResponse!.products.count))")
                    completion(jsonResponse?.products)

                    
                    /* Codable 版本代码 */
//                    let decoder = JSONDecoder()
//
//                    let jsonproInfoResponse = try decoder.decode(ProInfoResponse.self, from: moyaResponse.data)
//
//                    print("proinfo.swift商品列表json",jsonproInfoResponse)
//                    //print("proinfo.swift商品列表img",jsonproInfoResponse.products)
//                    completion(jsonproInfoResponse.products)
                    
                } catch  {
                     print("proinfo.swift商品列表json解析错误：",error)
                }
           
    
            case .failure:
                print("网络错误")
                completion(nil)
                
            }
        }
    }
}
